<div class="layui-fluid layui-anim admin-anim" id="admin-log" lay-title="系统日志">
  <div class="layui-row admin-container">
    <div class="layui-col-md12">
      <div class="layui-card">
        <div class="layui-card-body admin-table-full">
          <form class="layui-form layui-table-form" lay-filter="log-table-form">
            <div class="layui-row">
              <div class="layui-col-md10">
                <div class="layui-form-item">
                  <div class="layui-inline">
                    <label class="layui-form-label layui-form-label-sm">操作人</label>
                    <div class="layui-input-inline">
                      <input type="text" name="username" autocomplete="off" class="layui-input">
                    </div>
                  </div>
                  <div class="layui-inline">
                    <label class="layui-form-label layui-form-label-sm">操作时间</label>
                    <div class="layui-input-inline">
                      <input type="text" name="createdAt" id="createdAt" class="layui-input"/>
                    </div>
                  </div>
                  <div class="layui-inline">
                    <label class="layui-form-label layui-form-label-sm">操作描述</label>
                    <div class="layui-input-inline">
                      <input type="text" name="operation" autocomplete="off" class="layui-input">
                    </div>
                  </div>
                </div>
              </div>
              <div class="layui-col-md2 table-action-area">
                <div class="layui-btn layui-btn-sm layui-btn-primary table-action" id="query">
                  <i class="layui-icon">&#xe848;</i>
                </div>
                <div class="layui-btn layui-btn-sm layui-btn-primary table-action" id="reset">
                  <i class="layui-icon">&#xe79b;</i>
                </div>
                <div class="layui-btn layui-btn-sm layui-btn-primary table-action action-more"
                     shiro:hasAnyPermissions="log:delete,log:export">
                  <i class="layui-icon">&#xe875;</i>
                </div>
              </div>
            </div>
          </form>
          <table lay-filter="logTable" lay-data="{id: 'logTable'}"></table>
        </div>
      </div>
    </div>
  </div>
</div>
<script type="text/html" id="log-time">
  <div>
    {{# if (d.time < 500) { }}
    <span class="layui-badge admin-tag-green">{{d.time}} ms</span>
    {{# } else if (d.time < 1000) { }}
    <span class="layui-badge admin-tag-cyan">{{d.time}} ms</span>
    {{# } else if (d.time < 1500) { }}
    <span class="layui-badge admin-tag-orange">{{d.time}} ms</span>
    {{# } else { }}
    <span class="layui-badge admin-tag-red">{{d.time}} ms</span>
    {{# } }}
  </div>
</script>
<script type="text/html" id="log-option">
  <span shiro:lacksPermission="log:delete">
        <span class="layui-badge-dot admin-bg-orange"></span> 无权限
    </span>
  <a lay-event="del" shiro:hasPermission="log:delete"><i class="layui-icon admin-edit-area admin-red">&#xe7f9;</i></a>
</script>
<script data-th-inline="none" type="text/javascript">
  layui.use(['jquery', 'laydate', 'form', 'table', 'admin', 'dropdown'], function () {
    var $           = layui.jquery,
        laydate     = layui.laydate,
        admin       = layui.admin,
        form        = layui.form,
        table       = layui.table,
        dropdown    = layui.dropdown,
        $view       = $('#admin-log'),
        $query      = $view.find('#query'),
        $reset      = $view.find('#reset'),
        $delete     = $view.find('#delete'),
        $searchForm = $view.find('form'),
        sortObject  = {field: 'time', type: null},
        tableIns,
        createTimeFrom,
        createTimeTo;

    laydate.render({
       elem   : '#createdAt',
       range  : true,
       trigger: 'click'
     });

    form.render();
    initTable();

    dropdown.render({
      elem   : $view.find('.action-more'),
      click  : function (name, elem, event) {
        if (name === 'delete') {
          var checkStatus = table.checkStatus('logTable');
          if (!checkStatus.data.length) {
            admin.alert.warn('请勾选需要删除的日志');
          } else {
            admin.modal.confirm('删除日志', '确定删除所选日志？', function () {
              var logIds = [];
              layui.each(checkStatus.data, function (key, item) {
                logIds.push(item.id)
              });
              deleteLogs(logIds.join(','))
            });
          }
        }
        if (name === 'export') {
          var params      = $.extend(getQueryParams(), {field: sortObject.field, order: sortObject.type});
          params.pageSize = $view.find(".layui-laypage-limits option:selected").val();
          params.pageNum  = $view.find(".layui-laypage-em").next().html();
          admin.download(ctx + 'log/excel', params, '系统日志表.xlsx');
        }
      },
      options: [{
        name : 'delete',
        title: '删除日志',
        perms: 'log:delete'
      }, {
        name : 'export',
        title: '导出Excel',
        perms: 'log:export'
      }]
    });

    table.on('tool(logTable)', function (obj) {
      var data     = obj.data,
          layEvent = obj.event;
      if (layEvent === 'del') {
        admin.modal.confirm('删除日志', '确定删除该条系统日志？', function () {
          deleteLogs(data.id);
        });
      }
    });

    table.on('sort(logTable)', function (obj) {
      sortObject = obj;
      tableIns.reload({
                        initSort: obj,
                        where   : $.extend(getQueryParams(), {
                          field: obj.field,
                          order: obj.type
                        })
                      });
    });

    $query.on('click', function () {
      var params = $.extend(getQueryParams(), {field: sortObject.field, order: sortObject.type});
      tableIns.reload({where: params, page: {curr: 1}});
    });

    $reset.on('click', function () {
      $searchForm[0].reset();
      sortObject.type = 'null';
      createTimeTo    = null;
      createTimeFrom  = null;
      tableIns.reload({where: getQueryParams(), page: {curr: 1}, initSort: sortObject});
    });

    function initTable() {
      tableIns = admin.table.init({
                                    elem: $view.find('table'),
                                    id  : 'logTable',
                                    url : ctx + 'log/list',
                                    cols: [[
                                      {type: 'checkbox'},
                                      {field: 'username', title: '操作人'},
                                      {field: 'operation', title: '操作描述'},
                                      {field: 'time', title: '耗时', templet: "#log-time", sort: true},
                                      {field: 'method', title: '操作方法'},
                                      {field: 'params', title: '方法参数'},
                                      {field: 'ip', title: 'IP地址'},
                                      {field: 'location', title: '操作地点'},
                                      {field: 'createdAt', title: '创建时间', minWidth: 180, sort: true},
                                      {title: '操作', toolbar: '#log-option'}
                                    ]]
                                  });
    }

    function deleteLogs(logIds) {
      admin.get(ctx + 'log/delete/' + logIds, null, function () {
        admin.alert.success('删除系统日志成功');
        $query.click();
      });
    }

    function getQueryParams() {
      var createdAt = $searchForm.find('input[name="createdAt"]').val();

      if (createdAt) {
        createTimeFrom = createdAt.split(' - ')[0];
        createTimeTo   = createdAt.split(' - ')[1];
      }
      return {
        createTimeFrom     : createTimeFrom,
        createTimeTo       : createTimeTo,
        username           : $searchForm.find('input[name="username"]').val().trim(),
        operation          : $searchForm.find('input[name="operation"]').val().trim(),
        invalidate_ie_cache: new Date()
      };
    }
  })
</script>